/**
 * HessianSerializer.java
 * Created at 2016年4月16日
 * Created by Jackie Liu
 * Copyright (C) 2016 Jackie Liu, All rights reserved.
 */
package com.huabo.serialize;

import org.apache.commons.net.util.Base64;

import java.io.*;

/**
 * <p>ClassName: HessianSerializer</p>
 * <p>Description: Java序列化</p>
 * <p>Author: Jackie Liu</p>
 * <p>Date: 2016年4月16日</p>
 */
public class JavaSerializer implements Serializer {

	/* (non-Javadoc)
	 * <p>Title: getString</p>
	 * <p>Description: </p>
	 * @param object
	 * @return
	 * @see com.ssi.common.serialize.ObjectSerialize#getString(java.lang.Object)
	 */
	@Override
	public String getString(Object object) {
		if (object == null) {
			throw new NullPointerException();
		}
		byte[] byteArray = getByteArray(object);
		if (byteArray != null && byteArray.length > 0) {
			return new Base64().encodeToString(byteArray);
		}
		return null;
	}

	/* (non-Javadoc)
	 * <p>Title: getByteArray</p>
	 * <p>Description: </p>
	 * @param object
	 * @return
	 * @see com.ssi.common.serialize.ObjectSerialize#getByteArray(java.lang.Object)
	 */
	@Override
	public byte[] getByteArray(Object object) {
		if (object == null) {
			throw new NullPointerException();
		}
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = null;
		try {
			objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
			objectOutputStream.writeObject(object);  
			return byteArrayOutputStream.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (objectOutputStream != null) {
					objectOutputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
	        try {
				byteArrayOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * <p>Title: getObject</p>
	 * <p>Description: </p>
	 * @param serializeText
	 * @param clazz
	 * @return
	 * @see com.ssi.common.serialize.ObjectSerialize#getObject(java.lang.String, java.lang.Class)
	 */
	@Override
	public <T> T getObject(String serializeText, Class<T> clazz) {
		if (serializeText == null || "".equals(serializeText)) {
			throw new NullPointerException();
		}
		Base64 decoder = new Base64();
		byte[] byteArray = decoder.decode(serializeText);
		if (byteArray != null && byteArray.length > 0) {
			return getObject(byteArray, clazz);
		}
		return null;
	}

	/* (non-Javadoc)
	 * <p>Title: getObject</p>
	 * <p>Description: </p>
	 * @param serializeByteArray
	 * @param clazz
	 * @return
	 * @see com.ssi.common.serialize.ObjectSerialize#getObject(byte[], java.lang.Class)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T> T getObject(byte[] serializeByteArray, Class<T> clazz) {
		if (serializeByteArray == null || serializeByteArray.length <= 0) {
			throw new NullPointerException();
		}
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serializeByteArray);
        ObjectInputStream objectInputStream = null;
		try {
			objectInputStream = new ObjectInputStream(byteArrayInputStream);
			return (T) objectInputStream.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (objectInputStream != null) {
					objectInputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
	        try {
				byteArrayInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

}
